<?php
$baseURL = './CentralController.php?surveycode=Surveys';
require_once ROOT_PATH . '/lib/models/surveys/Answers.php';//Chinh Nguyen add
require_once ROOT_PATH . '/lib/models/hrfunct/EmpInfo.php';
require_once ROOT_PATH . '/lib/models/maintenance/Rights.php';
$rights = new Rights();
//Get survey right module MOD011
$locRights = $rights->getRights($_SESSION['userGroup'],'MOD011'); 

function getSurveyType($type){
	if($type ==0)
		return "Survey";
	elseif($type==1)
		return "Poll";
	else
		return "Quiz"; 	
}  
 ?>
<script type="text/javascript">
 function returnAdd() {
		location.href = '<?php echo $baseURL;?>&action=Define';
}
function returnDelete() {
		var check = false;
		with (document.frmSurveys) {
			for (var i=0; i < elements.length; i++) {
				if ((elements[i].type == 'checkbox') && (elements[i].checked == true)){
					check = true;
					break;
				}
			}

			if (check) {
				document.frmSurveys.action= "<?php echo $_SERVER['PHP_SELF']; ?>?surveycode=Surveys&action=DeleteSurveys";
				document.frmSurveys.submit();
			} else {
				alert("<?php echo $lang_Error_SelectAtLeastOneRecordToDelete; ?>");
			}
		}
	}
 </script>
<h2><?php echo $lang_Surveys_List; ?><hr/></h2>
<form id="frmSurveys" name="frmSurveys" method="post" action="">
<table border="0" cellpadding="0" cellspacing="0">
  <thead>
  	<tr>
		<th class="tableTopLeft"></th>
    	<th class="tableTopMiddle" width="200"></th>
    	<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" ></th>
    	<th class="tableTopMiddle" ></th>
    	<?php if($locRights['edit']) ?>
    	<th class="tableTopMiddle" ></th>
		<th class="tableTopRight"></th>
	</tr>
	<tr>
		<th class="tableMiddleLeft"></th>
    	<th  class="tableMiddleMiddle"><?php echo $lang_Survey_Name; ?></th>
    	<th class="tableMiddleMiddle">Survey type</th>
    	<th  class="tableMiddleMiddle"><?php echo $lang_Survey_Duration; ?></th>    	
    	<th class="tableMiddleMiddle"><?php echo $lang_Survey_Owner; ?></th>
    	<?php if($locRights['edit']) ?>
    	<th class="tableMiddleMiddle"><?php echo $lang_Survey_Action; ?></th>
		<th class="tableMiddleRight"></th>
	</tr>
  </thead>
  <tbody>
<?php
		
	
	
	$count =0;
	if (is_array($records)) {	
		$j = 0;	
		foreach ($records as $record) {
			//$surveyOwner = 
			if(!($j%2)) {
				$cssClass = 'odd';
			 } else {
			 	$cssClass = 'even';
			 }
			
		$answers = Answers::getTotalAnswersByUser($record->getId(),$_SESSION['empID']);		
			if($answers>0 ||($locRights['edit'] && $record->getSurveyOwner()== $_SESSION['empID'])){			
				$count++;
				 $j++; 
			?>
						  <tr>
						  	<td class="tableMiddleLeft"></td>
						    <td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=viewPersonalResult&id=<?php echo $record->getId(); ?>" target="rightMenu"><?php echo $record->getSurveyName(); ?></a></td>
						    <td class="<?php echo $cssClass; ?>"><?php echo getSurveyType($record->getSurveyType()); ?></td>
						    <td class="<?php echo $cssClass; ?>"><?php echo date('m/d/Y',strtotime($record->getSurveyFromDate()))."->".date('m/d/Y',strtotime($record->getSurveyToDate())); ?></td>
						    <td class="<?php echo $cssClass; ?>"><?php  echo  EmpInfo::getFullName($record->getSurveyOwner()); ?></td>
						    <?php if($locRights['edit'] && $record->getSurveyOwner()== $_SESSION['empID']) ?>
						    	<td class="<?php echo $cssClass; ?>"><a href="CentralController.php?surveycode=Surveys&action=ShowEditSurvey&id=<?php echo $record->getId(); ?>" target="rightMenu">Edit</td>
							<td class="tableMiddleRight"></td>
						  </tr>
			
			<?php
			}
		}
	}
?>
<?php if ($count==0){ ?>
	 <tr>
  	<td class="tableMiddleLeft"></td>
    <td >No survey avaible now</td>
    <td ></td>
    <td ></td>
    <td ></td>
    <?php if($locRights['edit']) ?>
    <td ></td>
	<td class="tableMiddleRight"></td>
  </tr>
<?php } ?>		
  </tbody>
  <tfoot>
  	<tr>
		<td class="tableBottomLeft"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomMiddle"></td>
		<?php if($locRights['edit']) ?>
		<td class="tableBottomMiddle"></td>
		<td class="tableBottomRight"></td>
	</tr>
  </tfoot>
</table>

